package com.hxh.controller;

import com.alibaba.fastjson.JSONArray;
import com.hxh.entity.output.ApiResult;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.springBoot.GenerationSpringBootAdapter;
import com.hxh.generater.vue.GenerationVueAdapter;
import com.hxh.util.MysqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ProjectName: AICode
 * @Package: com.example.controller.generation
 * @ClassName: GenerationToolsController
 * @Author: HXH
 * @Description: 非项目类的生成工具
 * @Date: 2024/3/21 15:29
 */
@RestController
@RequestMapping("/api/generationTools")
@Slf4j
public class GenerationToolsController {
    @Resource
    GenerationSpringBootAdapter generationSpringBootAdapter;

    @Autowired
    GenerationVueAdapter generationVueAdapter;

    /**
     * 返回数据是数组，每个数组是一个json对象
     * {name:xxx,type:xxx,code:xxx}
     * type:java | xml |javascript 三种格式
     *
     * @param sql
     * @param packageName
     * @param template
     * @return
     */
    @RequestMapping(value = "/codePreview", produces = "application/json;charset=utf-8")
    public ApiResult codePreview(@RequestParam String sql, @RequestParam String packageName,
                                 @RequestParam(required = false, defaultValue = "simple") String template) {
        try {
            ClassInfo classInfo = MysqlUtils.generationClassInfoByCreateTableSql(sql);
            JSONArray objects = generationSpringBootAdapter.generationCRUDTools(template, packageName, classInfo);
            return ApiResult.ok("ok", objects);
        } catch (Exception e) {
            log.error("非项目类的生成工具 生成crud异常", e);
            return ApiResult.fail(e);
        }

    }
}
